<template>
  <div class="userlook">
    <el-card>
      <el-tabs>
        <el-tab-pane label="登录账户设置">
          <el-form
            ref="form"
            :rules="rules"
            :model="form"
            label-width="100px"
            class="form"
          >
            <el-form-item label="姓名" prop="username">
              <el-input v-model="form.username" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="form.password" show-password />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submit">更新</el-button>
              <el-button @click="cancelClick">取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情">
          <Info :user-info="propUserInfo" @updateUserInfo="updateUserInfo" />
        </el-tab-pane>
        <el-tab-pane label="岗位信息">
          <Jpb />
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { sysUser, sysUserPut } from '@/api/user'
// import Info from './components/Info.vue'
// import Jpb from './components/Job.vue'
export default {
  components: {
    Info: () => import('./components/Info.vue'),
    Jpb: () => import('./components/Job.vue')
  },
  data() {
    return {
      userId: this.$route.params.id,
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }],
        password: [
          { required: true, message: '必填', trigger: 'change' },
          {
            min: 6,
            max: 12,
            message: '6-12位密码',
            trigger: 'change'
          }
        ]
      },
      userInfo: {},
      propUserInfo: {}
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysUser(this.userId)
      //   console.log(res)
      this.form.username = res.data.username
      this.propUserInfo = JSON.parse(JSON.stringify(res.data))
      this.userInfo = res.data
    },

    // 更新按钮点击事件
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          await sysUserPut({ ...this.userInfo, ...this.form })
          this.propUserInfo.username = this.form.username
          this.propUserInfo.password = this.form.password
          this.$message.success('修改成功')
        }
      })
    },

    // 取消按钮点击事件
    cancelClick() {
      this.$router.push('/employees')
    },

    // Info组件名字更新后，同步更新到当前组件
    updateUserInfo(obj) {
      this.userInfo = JSON.parse(JSON.stringify(obj))
      this.form.username = obj.username
    }
  }
}
</script>

<style lang="scss" scoped>
.userlook {
  padding: 15px;
  .form {
    padding-top: 50px;
    width: 600px;
    margin: 0 auto;
  }
}
</style>
